//
//  WYSettingController.m
//  Boss
//
//  Created by admin on 2018/7/3.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYSettingController.h"
#import "WYPersonMessageCell.h"
#import "WYAboutBOSSController.h"
#import "WYProposalController.h"
#import "WYLoginController.h"
#import "WYBaseNavigationController.h"
#import "CJMessageRemindController.h"
@interface WYSettingController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong) NSArray *nameArray;
@end

@implementation WYSettingController
-(NSArray *)nameArray
{
    if (!_nameArray) {
        _nameArray = @[@"消息提醒设置",@"清除缓存",@"版本信息",@"意见反馈",@"给个好评"];
    }
    return _nameArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //NSLog(@"%ld",class_getInstanceSize([ class]));
    
}
-(void)setupUI
{
    self.navigationItem.title = @"设置";
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYPersonMessageCell" bundle:nil] forCellReuseIdentifier:@"WYPersonMessageCell"];
    
    UIView *footerView = [[UIView alloc]init];
    footerView.frame = CGRectMake(0, 0, kScreenWidth, 200);
    self.tableView.tableFooterView = footerView;
    self.tableView.backgroundColor = [UIColor hexStringToColor:@"#f7f7f7" alpha:1.0];
    UIButton *exitBtn = [[UIButton alloc]init];
    [footerView addSubview:exitBtn];
    [exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(footerView);
    }];
    
    [exitBtn setBackgroundColor:kColorMainColor];
    [exitBtn setTitle:@"退出登录" forState:(UIControlStateNormal)];
    [exitBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    exitBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [exitBtn addTarget:self action:@selector(exitLoginIn) forControlEvents:(UIControlEventTouchUpInside)];
    
    
}
-(void)exitLoginIn
{
    WYLoginController *lvc = [[WYLoginController alloc]init];
    WYBaseNavigationController *nav =[[WYBaseNavigationController alloc]initWithRootViewController:lvc];
    [self presentViewController:nav animated:YES completion:nil];
    [WYStoreModule cleanToken];
    [self.tabBarController setSelectedIndex:0];
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    WYPersonMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYPersonMessageCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYPersonMessageCell" owner:nil options:nil].firstObject;
    }
    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    cell.backgroundColor = [UIColor whiteColor];
    
    
    
    
    
    cell.nameLabel.text = self.nameArray[indexPath.row];
    
    
    
    
    cell.rightNameLabel.text = @"";
    if (indexPath.row==1) {
        CGFloat size = [self readCacheSize];
        cell.rightNameLabel.text =[NSString stringWithFormat:@"%.2fM",size];
            cell.rightNameLabel.textColor = [UIColor hexStringToColor:@"#999999" alpha:1.0];
    }
    if (indexPath.row==2) {
        cell.rightNameLabel.text  = @"2.0";
        cell.rightNameLabel.textColor = [UIColor hexStringToColor:@"#999999" alpha:1.0];
    }
    
    return cell;
    
    
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row==3) {
//        WYAboutBOSSController *avc = [[WYAboutBOSSController alloc]init];
//        [self.navigationController pushViewController:avc animated:YES];
//    }
    
    if (indexPath.row==0) {
        CJMessageRemindController *mrc = [[CJMessageRemindController alloc]init];
        [self.navigationController pushViewController:mrc animated:YES];
    }
    
    
    
    if (indexPath.row==3) {
        WYProposalController *pvc = [[WYProposalController alloc]init];
        [self.navigationController pushViewController:pvc animated:YES];
    }
    if (indexPath.row==1) {
            [self clearFile];
            [tableView reloadData];
    }
    
    if (indexPath.row==2) {
          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id1412587317"]];
    }
    if (indexPath.row==4) {
       NSString *appid = @"1412587317";
        NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/viewContentsUserReviews?id=%@", appid];
        NSURL *url = [NSURL URLWithString:str];
        [[UIApplication sharedApplication] openURL:url];
    }
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}

-( float )readCacheSize
{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject];
    return [ self folderSizeAtPath :cachePath];
}
// 遍历文件夹获得文件夹大小，返回多少 M
- ( float ) folderSizeAtPath:( NSString *) folderPath{
    
    NSFileManager * manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        //获取文件全路径
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    }
    
    return folderSize/( 1024.0 * 1024.0);
    
}



// 计算 单个文件的大小
- ( long long ) fileSizeAtPath:( NSString *) filePath{
    NSFileManager * manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath :filePath]){
        return [[manager attributesOfItemAtPath :filePath error : nil] fileSize];
    }
    return 0;
}




- (void)clearFile
{
    NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
    NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath];
    //NSLog ( @"cachpath = %@" , cachePath);
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        //获取文件全路径
        NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p];
        
        if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) {
            [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error];
        }
    }
    [self toast:@"清理完成"];
    
    
    
    //读取缓存大小
    //float cacheSize = [self readCacheSize] *1024;
//    self.cacheSize.text = [NSString stringWithFormat:@"%.2fKB",cacheSize];
    
}












@end
